/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "sysvpn_utils.h"

#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <utils/debug.h>

void WriteStatus(char *info)
{
  if (!info) {
    return;
  }

  struct sockaddr_un address;
  int sockfd = -1;
  if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
    DBG1(DBG_LIB, "sysvpn socket fail");
    return;
  }

  address.sun_family = AF_UNIX;
  strncpy(address.sun_path, IPSECSOCKET, strlen(IPSECSOCKET));
  int nlength = sizeof(address);

  int result = connect(sockfd, (struct sockaddr *)&address, nlength);
  if (result == -1) {
    DBG1(DBG_LIB, "ensure the server is up");
    close(sockfd);
    return;
  }
  if (send(sockfd, info, strlen(info), 0) <= 0) {
    DBG1(DBG_LIB, "send data is fail");
    close(sockfd);
    return;
  }
  close(sockfd);
}

/**
 * check ipsec.conf file exist function for starter
 */
bool checkIpsecFile()
{
  bool bret = false;
  if (access(IPSECCONFFILE, F_OK) != -1) {
    bret = true;
  } else {
    bret = false;
  }
  return bret;
}
